package org.jrpc.spring;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.apache.commons.collections4.MapUtils;
import org.jrpc.common.baen.RpcRequest;
import org.jrpc.common.baen.RpcResponse;
import org.jrpc.common.codec.RpcDecoder;
import org.jrpc.common.codec.RpcEncoder;
import org.jrpc.server2.RpcServerHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by chenyuncong on 2018/7/3.
 */

@Component("springProtocolInitializer")
//@Qualifier("springProtocolInitializer")
public class StringProtocolInitalizer extends ChannelInitializer<SocketChannel> implements ApplicationContextAware {

   /* @Autowired
    StringDecoder stringDecoder;

    @Autowired
    StringEncoder stringEncoder;

    @Autowired
    ServerHandler serverHandler;*/

    /**
     * 存放 服务名 与 服务对象 之间的映射关系
     */
    protected Map<String, Object> handlerMap = new HashMap<>();

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
//        EventExecutor e1 = new DefaultEventExecutor();
//        pipeline.addLast("decoder", stringDecoder);
//        pipeline.addLast("handler", serverHandler);
//        pipeline.addLast("encoder", stringEncoder);
        pipeline.addLast(new RpcDecoder(RpcRequest.class)); // 解码 RPC 请求
        pipeline.addLast(new RpcEncoder(RpcResponse.class)); // 编码 RPC 响应
        pipeline.addLast(new RpcServerHandler(handlerMap)); // 处理 RPC 请求
//        pipeline.addLast(e1,null);
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        Map<String, Object> serviceBeanMap = ctx.getBeansWithAnnotation(Service.class);
        if (MapUtils.isNotEmpty(serviceBeanMap)) {
            for (Object serviceBean : serviceBeanMap.values()) {
                Service rpcService = serviceBean.getClass().getAnnotation(Service.class);
                System.out.println(serviceBean.getClass().getSuperclass());
                Class<?> interfaces[] = serviceBean.getClass().getInterfaces();//获得所实现的接口
                String serviceName = rpcService.value();
                for (Class<?> inte : interfaces) {//打印
                    serviceName=inte.getName();
                }
                handlerMap.put(serviceName, serviceBean);
            }
        }
        System.out.println(handlerMap.size());
    }


//    public StringDecoder getStringDecoder() {
//        return stringDecoder;
//    }
//
//    public void setStringDecoder(StringDecoder stringDecoder) {
//        this.stringDecoder = stringDecoder;
//    }
//
//    public StringEncoder getStringEncoder() {
//        return stringEncoder;
//    }
//
//    public void setStringEncoder(StringEncoder stringEncoder) {
//        this.stringEncoder = stringEncoder;
//    }
//
//    public ServerHandler getServerHandler() {
//        return serverHandler;
//    }
//
//    public void setServerHandler(ServerHandler serverHandler) {
//        this.serverHandler = serverHandler;
//    }


}
